package cz.acies.organizer.data.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import cz.acies.data.DBConnector;
import cz.acies.data.dao.DefaultDAO;
import cz.acies.data.persist.ModelObject;
import cz.acies.organizer.data.persist.CalendarTask;
import cz.acies.uniql.sql.DeleteQuery;
import cz.acies.uniql.sql.InsertQuery;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.UpdateQuery;
import cz.acies.uniql.sql.element.UniQLCondition;
import cz.acies.uniql.sql.element.UniQLWhere;
import cz.acies.uniql.sql.functions.SQLFunc;
import cz.acies.utils.UIDGenerator;

public class CalendarTaskDAO extends DefaultDAO
{

	public CalendarTask selectRecordById(String id) {
		try {
			List<ModelObject> records = super.selectById("calendar_task", id, CalendarTask.class);
			if (!records.isEmpty()) {
				ModelObject obj = records.get(0); 
				if (obj instanceof CalendarTask) {
					return (CalendarTask) obj; 
				}
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return null;		
	}

	public CalendarTask selectRecordByWhere(UniQLWhere where) {
		try {
			List<ModelObject> records = super.selectByWhere("calendar_task", where, CalendarTask.class);
			if (!records.isEmpty()) {
				ModelObject obj = records.get(0); 
				if (obj instanceof CalendarTask) {
					return (CalendarTask) obj; 
				}
			}
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return null;
	}

	public void doAppend(CalendarTask record) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnector.instance().conn();
			stmt = conn.createStatement();
			
			record.setId(UIDGenerator.createUID("CA.E")); 
			InsertQuery sql = new InsertQuery(dbmd())
			.into("calendar_task")
			.addParam("id", "" + record.getId())
			.addParam("folderId", "")
			.addParam("userId", "" + record.getUserId())
			.addParam("dateStart", "" + record.getDateStart())
			.addParam("dateEnd", "" + record.getDateEnd())
			.addParam("duration", "" + record.getDuration())
			.addParam("categories", "" + record.getCategories())
			.addParam("subject", "" + record.getSubject())
			.addParam("location", "" + record.getLocation())
			.addParam("description", "" + record.getDescription())
			.addParam("allDay", (record.isAllDay() ? "1": "0"))
			.addParam("recRule", "" + record.getRecRule())
			.addParam("writerId", "" + record.getUserId())
			.addParam("writeDate", new SQLFunc("NOW"))
			.addParam("editorId", "" + record.getUserId())
			.addParam("editDate", new SQLFunc("NOW"));

			//System.out.println(sql.toSQLString(true));
			stmt.executeUpdate(sql.toSQLString(true));
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			try { if (rs != null) rs.close(); } catch (Exception e) {}
			try { if (stmt != null) stmt.close(); } catch (Exception e) {}
			try { if (conn != null) conn.close(); } catch (Exception e) {}
		}		
	}
	
	public void doUpdate(CalendarTask record) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnector.instance().conn();
			stmt = conn.createStatement();
			
			String id = record.getId();
			UpdateQuery sql = new UpdateQuery(dbmd());
			sql.addFrom("calendar_task");
			sql.add(new UniQLCondition("id", "=", "'"+id+"'"));
			sql.addParam("dateStart", ""+record.getDateStart())
				.addParam("dateEnd", ""+record.getDateEnd())
				.addParam("duration", "" + record.getDuration())
				.addParam("categories", "" + record.getCategories())
				.addParam("subject", "" + record.getSubject())
				.addParam("location", "" + record.getLocation())
				.addParam("description", "" + record.getDescription())
				.addParam("allDay", (record.isAllDay() ? "1": "0"))
				.addParam("recRule", "" + record.getRecRule())
				.addParam("folderId", "")
				.addParam("userId", "" + record.getUserId())
				.addParam("editorId", "" + record.getUserId())
				.addParam("editDate", new SQLFunc("NOW"));

			stmt.executeUpdate(sql.toSQLString(true));
			
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			try { if (rs != null) rs.close(); } catch (Exception e) {}
			try { if (stmt != null) stmt.close(); } catch (Exception e) {}
			try { if (conn != null) conn.close(); } catch (Exception e) {}
		}
	}
	
	public void doDelete(CalendarTask record) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			QueryRunner qr = new QueryRunner(ds()); 
			conn = DBConnector.instance().conn();
			stmt = conn.createStatement();
			DeleteQuery sql = new DeleteQuery(dbmd())
			.addFrom("calendar_task")
			.add(new UniQLCondition("calendar_task.id", "=", "'" +record.getId()+"'"));
					 
			qr.update(sql.toSQLString(true));
			//stmt.executeUpdate(sql.toSQLString());
		} catch (Exception exc) {
			exc.printStackTrace();
		} finally {
			try { if (rs != null) rs.close(); } catch (Exception e) {}
			try { if (stmt != null) stmt.close(); } catch (Exception e) {}
			try { if (conn != null) conn.close(); } catch (Exception e) {}
		}
	}
}
